function validatePhone(rule, value, callback) {
    const reg = /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,1,3,5-8])|(18[0-9])|166|198|196|199|(147))\d{8}$/;
    if (!value) {
        callback(new Error('请输入手机号'));
    } else {
        if (!reg.test(value)) {
            callback(new Error('手机号不合法'));
        } else {
            callback();
        }
    }
}
function validateTel(rule, value, callback) {
    const reg = /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,1,3,5-8])|(18[0-9])|166|198|196|199|(147))\d{8}$/;
    const reg1 = /^\d{3,4}-?\d{7,8}$/;
    if (!value) {
        callback();
    } else {
        if (!reg.test(value) && !reg1.test(value)) {
            callback(new Error('联系方式不合法'));
        } else {
            callback();
        }
    }
}
function validatePassword(rule, value, callback) {
    if (value === "") {
        callback(new Error("请输入密码"));
    } else {
        if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/.test(value)) {
            callback("请输入8位以上，字母与数字组合的新密码");
        } else {
            callback();
        }
    }
};
export function validateStart(rule, value, callback) {
    if (!value) {
        callback(new Error('请输入开始时间'));
    } else {
        var _now_date = Date.now();
        if (+value - _now_date > 0) {
            callback();
        } else {
            callback('开始时间应大于当前时间');
        }
    }
}

export function validateEndTime(rule, value, callback) {
    if (!value) {
        callback(new Error('请输入结束时间'));
    } else {
        var _now_date = Date.now();
        if (+value - _now_date > 1000 * 60 * 10) {
            callback();
        } else {
            callback('距离结束时间不得小于10分钟');
        }
    }
}
export function validateNoticeStart(rule, value, callback) {
    if (!value) {
        callback(new Error('请输入开始时间'));
    } else {
        var _now_date = Date.now();
        if (+value - _now_date > 1000 * 60 * 10) {
            callback();
        } else {
            callback('距离开始时间不得小于10分钟');
        }
    }
}
export const inputRule = (message, required = true, fn, object, params) => {
    if (typeof fn === 'string') {
        if (fn === 'phone') {
            return [
                { required: required, validator: validatePhone, trigger: 'blur' }
            ]
        } else if (fn === 'telphone') {
            return [
                { required: required, validator: validateTel, trigger: 'blur' }
            ]
        } else if (fn === 'password') {
            return [
                { required: required, validator: validatePassword, trigger: 'blur' }
            ]
        } else if (fn === 'repassword') {
            return [
                {
                    required: required, validator: (rule, value, callback) => {
                        var list = params.split(',');
                        var obj = object;
                        for (var i = 0; i < list.length; i++) {
                            obj = obj[list[i]]
                        }
                        if (value === "") {
                            callback(new Error("请再次输入密码"));
                        } else if (value !== obj) {
                            callback(new Error("两次输入密码不一致!"));
                        } else {
                            callback();
                        }
                    }, trigger: 'blur'
                }
            ]
        } else if (fn === 'endtime') {
            return [
                { required: required, validator: validateEndTime, trigger: 'blur' }
            ]
        }
    } else if (typeof fn === 'undefined') {
        if (required) {
            return [
                {
                    required: true,
                    message: message,
                    tigger: 'blur'
                }
            ]
        } else {
            return [
                {
                    trigger: "blur", validator: (rule, value, callback) => {
                        if (!value) {
                            callback(new Error(message));
                        } else {
                            callback();
                        }
                    }
                }
            ]
        }

    } else if (typeof fn === 'function') {
        return [
            {
                required: required,
                message: message,
                tigger: 'blur'
            },
            { validator: fn, trigger: 'blur' }

        ]
    }
}
export const selectRule = (message, required = true, fn, object, params) => {
    if (typeof fn === 'string') {
        if (fn === 'array') {
            return [
                {
                    required: required,
                    message: message,
                    tigger: 'change',
                    type: "array"
                },
            ]
        } else if (fn === 'endtime') {
            return [
                { required: required, validator: validateEndTime, trigger: 'change' }
            ]
        } else if (fn === 'start') {
            return [
                { required: required, validator: validateStart, trigger: 'change' }
            ]
        } else if (fn === 'noticeStart') {
            return [
                { required: required, validator: validateNoticeStart, trigger: 'change' }
            ]
        } else if (fn === 'studyEnd') {
            return [
                {
                    required: required, validator: (rule, value, callback) => {
                        var list = params.split(',');
                        var obj = object;
                        for (var i = 0; i < list.length; i++) {
                            obj = obj[list[i]]
                        }
                        if (!value) {
                            callback(new Error("请输入结课时间"));
                        } else {
                            if (+value > +obj + 1000 * 60 * 60 * 12) {
                                callback();
                            } else {
                                callback("开课时间和结课时间最少间隔半天时间");
                            }
                        }
                    }, trigger: 'blur'
                }
            ]
        }

    } else if (typeof fn === 'undefined') {
        return [
            {
                required: required,
                message: message,
                tigger: 'change'
            },
        ]
    } else if (typeof fn === 'function') {
        return [
            {
                required: required,
                message: message,
                tigger: 'change'
            },
            { validator: fn, trigger: 'change' }
        ]
    } else {
        return []
    }

}
